<!-- 
 * @Author: guanpf 992093924@qq.om
 * @Date: 2023-06-29 17:18:54
 * @LastEditTime: 2023-09-10 14:08:27
 * @LastEditors: guanpf
 * @Description: 
 * @FilePath: /ZCGL_WX/src/views/main/asset/home.vue
 -->
<template>
  <div class="home">
    <div class="top">
      <div class="item-list">
        <div class="item" @click="goPage(1)">
          <div><img src="@/assets/images/asset/registion.png" alt="" /></div>
          <div>资产登记</div>
        </div>
        <div class="item" @click="goPage(2)">
          <div><img src="@/assets/images/asset/return.png" alt="" /></div>
          <div>资产归还</div>
        </div>
        <div class="item" @click="goPage(3)">
          <div><img src="@/assets/images/asset/lend.png" alt="" /></div>
          <div>资产借出</div>
        </div>
        <div class="item" @click="goPage(4)">
          <div><img src="@/assets/images/asset/transfer.png" alt="" /></div>
          <div>资产转移</div>
        </div>
        <div class="item" @click="goPage(5)">
          <div><img src="@/assets/images/asset/receive.png" alt="" /></div>
          <div>资产接收</div>
          <span class="num" v-if="receiveNumber">{{ receiveNumber }}</span>
        </div>
        <div class="item" @click="goPage(6)">
          <div><img src="@/assets/images/asset/inventory.png" alt="" /></div>
          <div>资产盘点</div>
        </div>
        <div class="item" @click="goPage(7)">
          <div><img src="@/assets/images/asset/receipt.png" alt="" /></div>
          <div>借据单</div>
        </div>
        <div class="item" @click="scanCode">
          <div><img src="@/assets/images/asset/scan_code.png" alt="" /></div>
          <div>扫一扫</div>
        </div>
        <!-- <div class="item">
            <div><img src="@/assets/images/asset/registion.png" alt=""></div>
            <div>统计</div>
        </div> -->
      </div>
      <CommonSearch
        placeholder="请输入资产数字ID、资产名称、使用人"
        @searchFun="searchFun"
      ></CommonSearch>

      <group class="filter-group">
        <popup-picker
          :key="1"
          title="资产分类"
          :data="classList"
          @on-change="changeList"
          v-model="classId"
          show-name
          placeholder="请选择资产分类"
        ></popup-picker>
        <popup-picker
          :key="2"
          title="位置"
          :data="placeList"
          @on-change="changeList"
          v-model="place"
          placeholder="请选择位置"
        ></popup-picker>
      </group>
      <!-- <div class="search">
        <group>
          <x-input v-model="name" placeholder="请输入资产数字ID、资产名称">
            <x-icon slot="label" type="ios-search" class="icon-red"></x-icon>
          </x-input>
        </group>
      </div> -->
      <div class="count">共计{{ total }}条结果</div>
    </div>
    <tab :line-width="2" active-color="rgb(80, 119, 185)" custom-bar-width="60px">
      <tab-item selected @on-item-click="handler">我创建的{{ createSum }}</tab-item>
      <tab-item @on-item-click="handler">我使用的{{ useSum }}</tab-item>
    </tab>
    <scroller
      class="scroll-list-home"
      lock-x
      height="calc(100% - 360px)"
      @on-scroll-bottom="onScrollBottom"
      ref="scrollerBottom"
      :scroll-bottom-offst="200"
    >
      <div class="list">
        <div
          class="item"
          v-for="(item, index) in list"
          :key="index"
          @click="toLog(item.assetId)"
        >
          <div class="title">
            <span :class="`tag-${item.borrowState} tag`">{{
              getStatus(item.borrowState)
            }}</span>
            <span class="name">{{ item.assetName }}</span>
            <!-- <ion-icon name="chevron-forward-outline"></ion-icon> -->
            <x-icon class="more" type="ios-arrow-right"></x-icon>
          </div>
          <div class="label">
            <span class="title">资产数字ID</span>
            <span class="name">{{ item.assetCode }}</span>
          </div>
          <div class="label">
            <span class="title">资产分类</span>
            <span class="name">{{ item.className }}</span>
          </div>
          <div class="label">
            <span class="title">使用人</span>
            <span class="name">{{ item.useName }}</span>
          </div>
        </div>
        <template v-if="total > list.length">
          <load-more tip="loading"></load-more>
        </template>
        <template v-else>
          <div class="load-complete">已加载全部</div>
        </template>
      </div>
    </scroller>
  </div>
</template>

<script>
import { Group, XInput, Tab, TabItem, Scroller, LoadMore, PopupPicker } from "vux";
import CommonSearch from "./components/search";
import AssetApi from "@/api/asset.js";
export default {
  components: {
    Group,
    XInput,
    CommonSearch,
    Scroller,
    LoadMore,
    PopupPicker, Tab, TabItem,
  },
  data() {
    return {
      load: false,
      name: "",
      page: 1,
      pageSize: 50,
      total: 10,
      list: [],
      classId: [],
      classList: [[]],
      placeList: [],
      place: [],
      receiveNumber: 0,
      userType:1,
      createSum:0,
      useSum:0
    };
  },
  mounted() {
    this.getList(2);
    this.getList();
    this.assetClass();
    this.findTenPlace();
    this.findUnconfirmAsset();
  },
  methods: {
    changeList(){
      this.load = false;
      this.getList()
    },
    handler(index) {
      this.userType = index + 1;
      this.load = false;
      this.getList()
    },
    // 扫码查资产ID
    scanCode() {
      let _this = this;
      wx.scanQRCode({
        desc: "scanQRCode desc",
        needResult: 1, // 默认为0，扫描结果由企业微信处理，1则直接返回扫描结果，
        scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是条形码（一维码），默认二者都有
        success: function (res) {
          //  res.resultStr
          this.$router.push({
            path: "./registration",
            query: {
              id: res.resultStr,
            },
          });
          // 回调
          // var result = res.resultStr;//当needResult为1时返回处理结果
        },
        error: function (res) {
          if (res.errMsg.indexOf("function_not_exist") > 0) {
            alert("版本过低请升级");
          }
        },
      });
    },
    findTenPlace() {
      AssetApi.findTenPlace().then((res) => {
        if (res.code == "0000") {
          this.placeList = [res.data] || [];
        }
      });
    },
    findUnconfirmAsset() {
      AssetApi.findUnconfirmAsset().then((res) => {
        if (res.code == "0000") {
          console.log(163, res.data);
          this.receiveNumber = res.data.order + res.data.transfer;
          if (this.receiveNumber > 99) {
            this.receiveNumber = "99+";
          }
        }
      });
    },
    assetClass() {
      let _params = {
        pageNum: 1,
        pageSize: 1000,
      };
      AssetApi.assetClass(_params).then((res) => {
        if (res.code == "0000") {
          let _classList =
            res.data.records.map((item) => {
              let temp = {
                name: item.name,
                value: item.id.toString(),
              };
              return temp;
            }) || [];
          this.classList = [_classList];
        }
      });
    },
    getStatus(state) {
      let text = "未出借";
      switch (state) {
        case 0:
          text = "未出借";
          break;
        case 1:
          text = "出借待确认";
          break;
        case 2:
          text = "出借中";
          break;
        case 3:
          text = "归还待确认";
          break;
        default:
          text = "未出借";
          break;
      }
      return text;
    },
    onScrollBottom() {
      if (this.total > this.list.length && this.load) {
        this.load = false;
        this.page++;
        this.getList();
      }
    },
    getList(type) {
      console.log(269,this.load)
      if (!this.load) {
        let _params = {
          userType:type?type:this.userType,
          key: this.name,
          // assetCode: this.name,
          // assetName: this.name,
          // userName: this.name,
          classId: this.classId.toString(),
          place: this.place.toString(),
          page: this.page,
          pageSize: this.pageSize,
        };
        AssetApi.getAssetList(_params)
          .then((res) => {
            if (res.code == "0000") {
              if(this.userType == 1){
                this.createSum = res.data.total
              }
              if(this.userType == 2 || type == 2){
                this.useSum = res.data.total
              }
              this.total = res.data.total;
              this.list = this.list.concat(res.data.records);
            }
          })
          .finally((res) => {
            this.load = true;
          });
      }
    },
    searchFun(name) {
      console.log(167, name);
      this.load = false;
      this.name = name;
      this.page = 1;
      this.list = [];
      this.getList();
      // 根据名称执行搜索操作
    },
    toLog(id) {
      this.$router.push({
        path: "log",
        query: {
          id: id,
          edit: true,
        },
      });
    },
    goPage(num) {
      let path = "";
      let query = {
        type: "",
      };
      switch (num) {
        case 1:
          path = "registration";
          break;
        case 2:
          path = "assetOpt";
          query.type = "return";
          break;
        case 3:
          path = "assetOpt";
          query.type = "lend";
          break;
        case 4:
          path = "assetOpt";
          query.type = "transfer";
          break;
        case 5:
          path = "receive";
          break;
        case 6:
          path = "inventory";
          break;
        case 7:
          path = "promissoryNote";
          break;

        default:
          path = "assetOpt";
          break;
      }
      this.$router.push({ path, query });
    },
  },
};
</script>

<style lang="less" scoped>
.scroll-list-home {
  -webkit-overflow-scrolling:touch;
  // overscroll-behavior-y: contain;
}
/deep/.top {
  .item-list {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    flex-wrap: wrap;
    padding: 10px 20px;
    gap: 0 35px;
    .item {
      padding: 10px 0;
      position: relative;
      width: 57px;
      text-align: center;
      :nth-child(1) {
        width: 30px;
        height: 30px;
        border-radius: 15px;
        display: flex;
        justify-content: center;
        align-items: center;
        margin: 0 auto;
        img {
          height: 16px;
          width: 16px;
          border-radius: 0;
        }
      }
      :nth-child(2) {
        margin-top: 5px;
      }

      .num {
        position: absolute;
        top: -2px;
        right: -10px;
        display: inline-block;
        background: #ec6240;
        color: #fff;
        padding: 2px 4px;
        border-radius: 10px;
        font-size: 12px;
        min-width: 10px;
        text-align: center;
      }
    }
    :nth-child(1) {
      :nth-child(1) {
        background: rgb(111, 165, 242);
      }
    }
    :nth-child(2) {
      :nth-child(1) {
        background: rgb(104, 195, 154);
        img {
          width: 26px;
          height: 26px;
          border-radius: 15px;
        }
      }
    }
    :nth-child(3) {
      :nth-child(1) {
        background: rgb(123, 109, 246);
      }
    }
    :nth-child(4n) {
      margin-right: 0;
      :nth-child(1) {
        background: rgb(236, 98, 64);
      }
    }
    :nth-child(5) {
      :nth-child(1) {
        background: rgb(97, 126, 245);
      }
    }
    :nth-child(6) {
      :nth-child(1) {
        background: rgb(67, 80, 175);
      }
    }
    :nth-child(7) {
      :nth-child(1) {
        background: rgb(214, 87, 146);
      }
    }
    :nth-child(8) {
      :nth-child(1) {
        background: rgb(214, 87, 146);
      }
    }
  }

  .count {
    margin: 10px 20px;
  }
}
.list {
  height: auto;
  background: #f1f1f1;
  padding: 10px;
  -webkit-overflow-scrolling:touch;
  .item {
    padding: 10px 15px;
    border-radius: 8px;
    background: #fff;
    margin-bottom: 10px;

    .title {
      height: 30px;
      line-height: 30px;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      position: relative;
      .tag {
        color: #fff;
        position: relative;
        display: inline-block;
        padding: 0 3px 0 6px;
        font-size: 12px;
        line-height: 20px;
        height: 20px;
        border-radius: 4px 0 0 4px;
        &::after {
          border-left: 10px solid transparent;
          border-right: 10px solid transparent;
          //   border-top: 8px solid red;
          content: "";
          position: absolute;
          top: 6px;
          right: -14px;
          width: 0;
          z-index: 99;
          -webkit-transform: rotate(270deg);
          transform: rotate(270deg);
        }
      }
      .tag-1,
      .tag-0 {
        background: rgb(87, 184, 135);
        &::after {
          border-top: 8px solid rgb(87, 184, 135);
        }
      }
      .tag-2 {
        background: rgb(53, 120, 230);
        &::after {
          border-top: 8px solid rgb(53, 120, 230);
        }
      }
      .tag-3 {
        background: rgb(240, 153, 82);
        &::after {
          border-top: 8px solid rgb(240, 153, 82);
        }
      }
      .name {
        display: inline-block;
        margin-left: 20px;
      }
      .more {
        position: absolute;
        right: 0px;
      }
    }

    .label {
      display: flex;
      justify-content: flex-start;
      height: 30px;
      line-height: 30px;
      color: rgb(127, 127, 127);

      .title {
        display: inline-block;
        width: 100px;
      }
    }
  }
}
.load-complete {
  text-align: center;
  height: 40px;
  line-height: 40px;
  color: #666;
}
.filter-group {
  margin-top: 10px;
}
</style>
